﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
using System.Data.Entity;
namespace BenefitHelper.Data.Month
{
    /// <summary>
    /// 经纪公司交易信息
    /// </summary>
    public class BrokerageMonthTradeInfo
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int BrokerId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Broker Broker { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }


        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int month, int year, DB.DBManager db)
        {
            var query = from t in db.BrokerageMonthTradeInfo where t.Month == month && t.Year == year select t;
            foreach (BrokerageMonthTradeInfo d in query)
            {
                db.BrokerageMonthTradeInfo.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 初始化经纪公司月收益情况
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void InitBrokerMonthTradeInfo(int month, int year, DB.DBManager db)
        {
            BrokerageMonthTradeInfo binfo = new BrokerageMonthTradeInfo();
            var query = db.BrokerageDayTradeInfo.Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month);
            var query2 = db.BrokerageMonthTradeInfo.Where(a => a.Year == year).Where(a => a.Month == month);
            if (query2.Count() > 0)
            {
                binfo = query2.First();
            }
            binfo.BillCount = query.Sum(a => a.BillCount);
            binfo.BrokerId = query.First().BrokerId;
            binfo.DayCount = query.Sum(a => a.DayCount);
            binfo.Free = query.Sum(a => a.Free);
            binfo.LoseCount = query.Sum(a => a.LoseCount);
            binfo.Month = month;
            binfo.Profit = query.Sum(a => a.Profit);
            binfo.SumLost = query.Sum(a => a.SumLost);
            binfo.SumWin = query.Sum(a => a.SumWin);
            binfo.UsedMargin = query.Sum(a => a.UsedMargin);
            binfo.WinCount = query.Sum(a => a.WinCount);
            binfo.Year = year;
            if (query2.Count() == 0)
            {
                db.BrokerageMonthTradeInfo.Add(binfo);
            }
            db.SaveChanges();
        }

        public List<BrokerageMonthTradeInfo> GetBrokerageMonthTradeInfo(int brokerid, int year, int month, DB.DBManager db)
        {
            var query = db.BrokerageMonthTradeInfo.Where(a => a.BrokerId == brokerid).Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Broker);
            return query.ToList();
        }

        public List<BrokerageMonthTradeInfo> GetBrokerageMonthTradeInfo(int year, int month, DB.DBManager db)
        {
            var query = db.BrokerageMonthTradeInfo.Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Broker);
            return query.ToList();
        }
    }
}